const Koa = require('koa')
const router = require('koa-router')();
const app = new Koa()
const MainData = require('./Data/mainData/MainData.json')
const ServerData = require('./Data/serverData/ServerData.json')
const MyData = require('./Data/myData/MyData.json')
const InfoData = require('./Data/infoData/InfoData.json')
const cors = require('koa2-cors')
const Mock = require('mockjs')
const Random = Mock.Random
// const querystring = require('querystring');

app.use(cors({
    origin: function (ctx) { //设置允许来自指定域名请求
        // if (ctx.url === '/test') {
        return '*'; // 允许来自所有域名请求
        // }
        // return 'http://localhost:3000'; //只允许http://localhost:8080这个域名的请求
    },
    maxAge: 5, //指定本次预检请求的有效期，单位为秒。
    credentials: true, //是否允许发送Cookie
    allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'], //设置所允许的HTTP请求方法
    allowHeaders: ['Content-Type', 'Authorization', 'Accept'], //设置服务器支持的所有头信息字段
    exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'] //设置获取其他自定义字段
}))
router.get('/home/main', async (ctx) => {
    ctx.response.body = {
        success: true,
        data: MainData
    }
})

router.get('/home/server', async (ctx) => {
    ctx.response.body = {
        success: true,
        data: ServerData
    }
})

router.get('/home/my', async (ctx) => {
    ctx.response.body = {
        success: true,
        data: MyData
    }
})

router.get('/home/info', async (ctx) => {
    ctx.response.body = {
        success: true,
        data: InfoData
    }
})

router.get('/home/list', async (ctx) => {

    let {
        limit = 20, page = 1
    } = ctx.request.query
    console.log(limit, page, "###");
    console.log(ctx.request.query, "----------------");
    // 根据limit 和page 做数据筛选 

    // 参数   page limit
    let data = Mock.mock({
        'list|20': [{

            'id': "@increment",
            "title": "@ctitle(15,20)",
            "label": "@ctitle(3,5)",
            "imgsrc": Random.img('94x107')
        }],
        'text1|20': [{
            'id': "@increment",
            "title": "@ctitle(15,20)",
            'writer': '@ctitle(3,5)',
            'time': '@time(MM-dd HH:mm)',
            'imgsrc1': '@img(110x80)',
            'imgsrc2': '@img(110x80)',
            'imgsrc3': '@img(110x80)',
            'imgsrc4': '@img(110x120)',
            'type|1': Random.range(1, 4, 1),
        }],
        'text2|20': [{
            'id': "@increment",
            "title": "@ctitle(20,25)",
            'writer': '@ctitle(3,5)',
            'time': '@time(M-d H:m:s)',

        }]
    })
    ctx.body = {
        success: true,
        data
    }
})

router.get('/home/comments', async (ctx) => {

    // 根据limit 和page 做数据筛选 
    // 参数   page limit

    // {"success":true,"data":{"array":{"list1":{"id":1,"writer":"个细研"}}}}
    // {"success":true,"data":{"array":{"list1":{"id":1,"writer":"国栋"}}}}
    let data2 = {
        'array': [{
            // 'id': "@increment",
            //     'writer': '@ctitle(3,5)',
            //     'time': '@datetime("y-MM-dd HH:mm:ss")',
            //     'imgsrc1': '@img(250x180)',
            //     'imgsrc2': '@img(89x100)',
            //     'imgsrc3': '@img(89x100)',
            //     'text': '@cparagraph(0,5)',
            //     // Random.cparagraph(2)
            //     "goodnumber|1-10": 10,
            //     'commentsnumber|1-10': 10,

            'list1': [{
                id: 01,
                'writer': '国栋01',
                'time': '000000',
                'text': '000000',
                "goodnumber": 7,
                'commentsnumber': 4,

            }, {
                id: 02,
                'writer': '国栋02'
            }],

        }, {

            'list1': [{
                id: 11,
                'writer': '国栋11',
                'time': '01-08 13:24:13',
                'text': '整体看下单子吧，大家觉得有没有多余的浪费',
                "goodnumber": 7,
                'commentsnumber': 3,
            }, {
                id: 12,
                'writer': '国栋12',
                'time': '01-08 13:24:13',
                'text': '整体看下单子吧，大家觉得有没有多余的浪费',
                "goodnumber": 7,
                'commentsnumber': 6,
            }],
        }, {

            'list1': [{
                id: 21,
                'writer': '国栋21'
            }, {
                'id': 22,
                'writer': '国栋22'
            }],
        }, {

            'list1': [{
                id: 31,
                'writer': '国栋31'
            }, {
                id: 32,
                'writer': '国栋32'
            }],
        }, {

            'list1': [{
                id: 41,
                'writer': '国栋41'
            }, {
                id: 42,
                'writer': '国栋42'
            }],
        }, {

            'list1': [{
                id: 51,
                'writer': '国栋51'
            }, {
                id: 52,
                'writer': '国栋52'
            }],
        }, {

            'list1': [{
                id: 61,
                'writer': '国栋61'
            }, {
                id: 62,
                'writer': '国栋62'
            }],
        }, {

            'list1': [{
                id: 71,
                'writer': '国栋71'
            }, {
                id: 72,
                'writer': '国栋72'
            }],
        }, {

            'list1': [{
                id: 81,
                'writer': '国栋81'
            }, {
                id: 82,
                'writer': '国栋82'
            }],
        }]
    }

    let data1 = Mock.mock({
        'array|8': [{
            'list1|20': [{
                'id': "@increment",
                'writer': '@ctitle(3,5)',
                'time': '@datetime("y-MM-dd HH:mm:ss")',
                'imgsrc1': '@img(250x180)',
                'imgsrc2': '@img(89x100)',
                'imgsrc3': '@img(89x100)',
                'text': '@cparagraph(0,5)',
                // Random.cparagraph(2)
                "goodnumber|1-10": 10,
                'commentsnumber|1-10': 10,
            }]
        }]
    })
    // console.log(data1, 'data1-----------------');
    ctx.body = {
        success: true,
        data: data2
    }
})

// router.get('/detail/:', async (ctx) => {
//     console.log(ctx.query.data, '========')  //      ?
//     console.log(ctx.params,'--------');     //       :
//     const { id } = ctx.params
//     if(!id){
//         ctx.response.body = {
//             success:false,
//             msg:'请求有误'
//         }
//     }
//     // let category =  ctx.query.data || ''
//     ctx.response.body = {
//         success:true,
//         data:Mock.mock({
//             id,
//             title: '@ctitle(5, 10)',
//             price: '@float(60, 100)'
//         })
//     }
// })


app
    .use(router.routes())
    .use(router.allowedMethods())
// 1. http服务
// 2. 简单的路由模块
// 3. cors
// 4. 返回数据

app.listen(9090, () => {
    console.log('server is running in port 9090')
})